package com.gopay.common.cps.dao.aggregatepayment.impl;

import com.gopay.common.base.dao.impl.BaseReadDAOImpl;
import com.gopay.common.cps.dao.aggregatepayment.DicGopayApQueryDAO;
import com.gopay.common.domain.aggregatepayment.ApCustInfo;
import com.gopay.common.domain.aggregatepayment.DicGopayAp;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("dicGopayApQueryDAO")
public class DicGopayApQueryDAOImpl extends BaseReadDAOImpl<DicGopayAp,Integer> implements DicGopayApQueryDAO{

    @Override
    public List<DicGopayAp> getDicGopayApByParentCode(DicGopayAp dicGopayAp) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(DicGopayAp.class);
        if(StringUtils.isNotBlank(dicGopayAp.getType())){
            detachedCriteria.add(Restrictions.eq("type", dicGopayAp.getType()));
        }
        if(StringUtils.isNotBlank(dicGopayAp.getParentCode())){
            detachedCriteria.add(Restrictions.eq("parentCode", dicGopayAp.getParentCode()));
        }
        return findByCriteria(detachedCriteria);
    }

    @Override
    public DicGopayAp getDicGopayApByCode(DicGopayAp dicGopayAp) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(DicGopayAp.class);
        if(StringUtils.isNotBlank(dicGopayAp.getType())){
            detachedCriteria.add(Restrictions.eq("type", dicGopayAp.getType()));
        }

        if(StringUtils.isNotBlank(dicGopayAp.getCode())){
            detachedCriteria.add(Restrictions.eq("code", dicGopayAp.getCode()));
        }
        List<DicGopayAp> dicGopayApList = findByCriteria(detachedCriteria);
        if(dicGopayApList!=null&&!dicGopayApList.isEmpty()){
            return dicGopayApList.get(0);
        }
        return null;
    }
}
